package net.edwardstx.hibernateutils.configurators;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.cfg.AnnotationConfiguration;

import net.edwardstx.hibernateutils.HibernateSettings;


public class HibernateSettingsImpl implements HibernateSettings{

	private String stringJDBCServerURL = null;
	private String stringJDBCDriver = null;
	private String stringJDBCUsername = null;
	private String stringJDBCPassword = null;
	private String stringHibernateDialect = null;
	private Map<String,String> mapOfPropertyOverrides = null;
	
	
	public HibernateSettingsImpl(){
	}
	
	public HibernateSettingsImpl(String stringJDBCServerURLNew,
	                             String stringJDBCDriverNew,
	                             String stringJDBCUsernameNew,
	                             String stringJDBCPasswordNew,
	                             String stringHibernateDialectNew) {
		this(stringJDBCServerURLNew, stringJDBCDriverNew, stringJDBCUsernameNew, stringJDBCPasswordNew, stringHibernateDialectNew, null);
	}
	
	public HibernateSettingsImpl(String stringJDBCServerURLNew,
	                             String stringJDBCDriverNew,
	                             String stringJDBCUsernameNew,
	                             String stringJDBCPasswordNew,
	                             String stringHibernateDialectNew,
	                             Map<String,String> mapOfPropertyOverrides){
		this.setJDBCServerURL(stringJDBCServerURLNew);
		this.setJDBCDriver(stringJDBCDriverNew);
		this.setJDBCUsername(stringJDBCUsernameNew);
		this.setJDBCPassword(stringJDBCPasswordNew);
		this.setHibernateDialect(stringHibernateDialectNew);
		this.setPropertyOverrides(mapOfPropertyOverrides);
	}
	@Override
	public String getHibernateDialect() {
		return this.stringHibernateDialect;
	}

	public void setHibernateDialect(String stringHibernateDialectNew) {
		this.stringHibernateDialect = stringHibernateDialectNew;
	}

	@Override
	public String getJDBCDriver() {
		return this.stringJDBCDriver;
	}

	public void setJDBCDriver(String stringJDBCDriverNew) {
		this.stringJDBCDriver = stringJDBCDriverNew;
	}

	@Override
	public String getJDBCPassword() {
		return this.stringJDBCPassword;
	}

	public void setJDBCPassword(String stringJDBCPasswordNew) {
		this.stringJDBCPassword = stringJDBCPasswordNew;
	}

	@Override
	public String getJDBCServerURL() {
		return this.stringJDBCServerURL;
	}

	public void setJDBCServerURL(String stringJDBCServerURLNew) {
		this.stringJDBCServerURL = stringJDBCServerURLNew;
	}

	@Override
	public String getJDBCUsername() {
		return this.stringJDBCUsername;
	}

	public void setJDBCUsername(String stringJDBCUsernameNew) {
		this.stringJDBCUsername = stringJDBCUsernameNew;
	}

	@Override
	public Map<String, String> getPropertyOverrides() {
		return this.mapOfPropertyOverrides;
	}
	
	public void setPropertyOverrides(Map<String, String> mapOfPropertyOverridesNew) {
		if(mapOfPropertyOverridesNew == null) {
			mapOfPropertyOverridesNew = new HashMap<String,String>();
		}
		this.mapOfPropertyOverrides = mapOfPropertyOverridesNew;
	}

	@Override
	public AnnotationConfiguration configure(AnnotationConfiguration config) {
		config.setProperty("hibernate.connection.url",getJDBCServerURL());
		config.setProperty("hibernate.connection.driver_class",getJDBCDriver());
		config.setProperty("hibernate.connection.username",getJDBCUsername());
		config.setProperty("hibernate.connection.password",getJDBCPassword());
		config.setProperty("hibernate.dialect",getHibernateDialect());
//		 Apply any property overrides configured in the HibernateSettings object
		if(getPropertyOverrides() != null)
			for(String stringPropertyName : getPropertyOverrides().keySet()) 
				config.setProperty(stringPropertyName, getPropertyOverrides().get(stringPropertyName));
		return config;
	}
}
